package com.joesem.sale.client.logic.home

import android.app.Application
import com.blankj.utilcode.util.NotificationUtils
import com.igexin.sdk.PushManager
import com.joesem.sale.client.base.BaseViewModel
import com.joesem.sale.client.base.request
import com.joesem.sale.client.base.toast
import com.joesem.sale.client.db.LoginTable
import com.joesem.sale.client.network.data.AccountMode
import com.joesem.sale.client.network.data.NoticeCountMode
import com.joesem.sale.client.network.data.OrderRankingMode
import com.joesem.sale.client.network.data.OrderStatsNumMode


class MainViewModel(application: Application) : BaseViewModel(application) {
    fun noticeCunt() {
        request(request = {
            noticeCount()
        }, response = {
            it.data?.let {
                actionIntent(NoticeCountIntent(it))
            }
        }, responseFailed = {

        })
    }

    fun orderStatsNum() {
        request(request = {
            orderStatsNum()
        }, response = {
            it.data?.let {
                actionIntent(OrderStatusNumCountIntent(it))
            }
        }, responseFailed = {

        })
    }

    fun orderRanking() {
        request(request = {
            orderRanking()
        }, response = {
            it.data?.let {
                actionIntent(OrderRankingCountIntent(it.filterNotNull()))
            }
        }, responseFailed = {

        })
    }

    fun accountInfo() {
        request(request = {
            account()
        }, response = {
            it.data?.let {
                actionIntent(AccountIntent(it))
            }
        }, responseFailed = {

        })
    }

    fun logout() {
        request(isLoading = true, request = {
            logout()
        }, response = {
            LoginTable.table.clearAccount()
            PushManager.getInstance().turnOffPush(getApplication())
            NotificationUtils.cancelAll()
            actionIntent(LogoutIntent)
        }, responseFailed = {
            toast(it.message)
        })
    }

    class NoticeCountIntent(val point: NoticeCountMode)
    class OrderStatusNumCountIntent(val num: OrderStatsNumMode)
    class OrderRankingCountIntent(val data: List<OrderRankingMode>)
    class AccountIntent(val data: AccountMode)
    object LogoutIntent
}